package com.zbkj.admin.controller.region;

import com.zbkj.common.annotation.LogControllerAnnotation;
import com.zbkj.common.enums.MethodType;
import com.zbkj.common.model.admin.SystemAdmin;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.SystemAdminRequest;
import com.zbkj.common.request.adminregion.AdminRegionUserAddRequest;
import com.zbkj.common.response.zfbadminregion.ZfbAdminRegionUserResponse;
import com.zbkj.common.result.CommonResult;
import com.zbkj.common.utils.SecurityUtil;
import com.zbkj.service.service.adminregion.IZfbAdminRegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName: ZfbAdminRegionController
 * @Description: TODO
 * @Author: fanbing
 * @Date: 2024/7/27 12:58
 * @Version: V1.0
 **/
@Slf4j
@RestController
@RequestMapping("api/admin/region/adminRegion")
@Api(tags = "行政区域管理") //配合swagger使用
public class RegionAdminUserController {


    @Autowired
    private IZfbAdminRegionService zfbAdminRegionService;

    @LogControllerAnnotation(intoDB = true, methodType = MethodType.ADD, description = "新增后台管理员")
    @ApiOperation(value = "新增行政区域管理员")
    @RequestMapping(value = "/saveAccount", method = RequestMethod.POST)
    public CommonResult<String> saveAccount(@RequestBody AdminRegionUserAddRequest systemAdminAddRequest) {

        SystemAdmin currentUser = SecurityUtil.getLoginUserVo().getUser();
        systemAdminAddRequest.setParentId(currentUser.getId());
        if (zfbAdminRegionService.platformSaveAccount(systemAdminAddRequest)) {
            return CommonResult.success("添加管理员成功");
        }
        return CommonResult.failed("添加管理员失败");
    }

    @ApiOperation(value = "行政区域超级管理员账号列表")
    @RequestMapping(value = "/accountList", method = RequestMethod.GET)
    public CommonResult<CommonPage<ZfbAdminRegionUserResponse>> getList(@Validated SystemAdminRequest systemAdminRequest, PageParamRequest pageParamRequest) {
        CommonPage<ZfbAdminRegionUserResponse> systemAdminCommonPage = CommonPage.restPage(zfbAdminRegionService.getPlatformList(systemAdminRequest, pageParamRequest));
        return CommonResult.success(systemAdminCommonPage);
    }


    @ApiOperation(value = "行政区域超级管理员账号详情")
    @RequestMapping(value = "/accountDetail/{id}", method = RequestMethod.GET)
    public CommonResult<ZfbAdminRegionUserResponse> getAccountDetail(@PathVariable(value = "id") Integer id) {
        ZfbAdminRegionUserResponse regionUser = zfbAdminRegionService.getAdminRegionUser(id);
        return CommonResult.success(regionUser);
    }

    @ApiOperation(value = "修改行政区域超级管理员账号")
    @RequestMapping(value = "/updateAccount", method = RequestMethod.POST)
    public CommonResult updateAccount(@RequestBody AdminRegionUserAddRequest systemAdminAddRequest) {
        return CommonResult.success(zfbAdminRegionService.platformUpdateAccount(systemAdminAddRequest));
    }


    @ApiOperation(value = "删除行政区域超级管理员账号")
    @RequestMapping(value = "/deleteAccount/{id}", method = RequestMethod.GET)
    public CommonResult deleteAccount(@PathVariable(value = "id") Integer id) {
        return CommonResult.success(zfbAdminRegionService.deleteAdminRegionUser(id));
    }


    @ApiOperation(value = "获取当前用户管理区域")
    @RequestMapping(value = "/getCurrentUserRegion", method = RequestMethod.GET)
    public CommonResult getCurrentUserRegion() {
        return CommonResult.success(zfbAdminRegionService.getCurrentUserRegion());
    }
}
